import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import {crx} from '@crxjs/vite-plugin'
import manifest from './manifest.json'
import path from 'path'

// https://vite.dev/config/
export default defineConfig({
    plugins: [
        vue(),
        crx({manifest}),
    ],
    build: {
        rollupOptions: {
            input: {
                popup: 'src/popup/index.html',
                options: 'src/options/options.html',
                // 内容脚本需要显式声明
                'content-script': 'src/content/previewJson.js',
                // 添加关键词高亮脚本
                'keyword-highlight': 'src/content/keywordHighlight.js'
            },
            output: {
                assetFileNames: (assetInfo) => {
                    // 确保CSS文件有正确的输出路径
                    if (assetInfo.name && assetInfo.name.includes('.css')) {
                        return '[name][extname]'
                    }
                    return 'assets/[name]-[hash][extname]'
                },
                // 确保内容脚本有正确的命名
                entryFileNames: (chunkInfo) => {
                    if (chunkInfo.name === 'content-script') {
                        return '[name].js'
                    }
                    return 'assets/[name]-[hash].js'
                }
            }
        }
    },

    legacy: {
        skipWebSocketTokenCheck: true,
    },
    resolve: {
        alias: {
            '@': path.resolve(__dirname, './src')// 确保引用路径正确
        }
    }
})
